-- SPDX-License-Identifier: GPL-3.0-or-later

-- BasicAi base class.
-- Do nothing.

---@class BasicAi: Object
---@field public room Room
---@field public player ServerPlayer
---@field public command string
---@field public jsonData string
---@field public cb_table table<string, fun(self: BasicAi, jsonData: string)>
local BasicAi = class("BasicAi")

function BasicAi:initialize(player)
  self.room = RoomInstance
  self.player = player
  local cb_t = {}
  -- default strategy: print command and data, then return ""
  setmetatable(cb_t, {
    __index = function()
      return function()
        print(self.command, self.jsonData)
        return ""
      end
    end,
  })
  self.cb_table = cb_t
end

function BasicAi:readRequestData()
  self.command = self.player.ai_data.command
  self.jsonData = self.player.ai_data.jsonData
end

function BasicAi:makeReply()
  -- self.room:delay(100)
  Self = self.player
  -- local start = os.getms()
  -- self.delay_t = 700
  -- if #self.player.player_cards[Player.Hand] > 14 then
  --   self.delay_t = 800
  -- end
  -- if self.command == "AskForGeneral" then
  --   self.delay_t = 100
  -- end
  if self.command ~= "PlayCard" then
    self.upmaxslash = true
  end
  -- print(self.upmaxslash)
  -- print(self.delay_t)
  local ret = self.cb_table[self.command] and self.cb_table[self.command](self, self.jsonData,self.upmaxslash) or "__cancel"
  -- local to_delay = self.delay_t - (os.getms() - start) / 1000
  -- p(self.command)
  -- p(self.jsonData)
  self.room:delay(self.delay_t)
  return ret
end

return BasicAi
